import { Injectable } from '@angular/core';
import { DfHTTP } from '../../lib/network/network.service';
import { NetworkConfig, TasksResponse, ApproveTaskResponse } from '../../app/app.service';

@Injectable()
export class TasksService {

    constructor(private dfHTTP: DfHTTP) { }

    getAllTask(): Promise<any> {

        return this.dfHTTP.post(NetworkConfig.paths.getTodo, { "page": "1", "size": "20" })
            .then((res: TasksResponse) => res);

    }

    approveTask(taskId: string): Promise<any> {

        return this.dfHTTP.post(NetworkConfig.paths.approve, { "task_id": taskId })
            .then((res: ApproveTaskResponse) => res);

    }

    rejectTask(taskId: string): Promise<any> {

        return this.dfHTTP.post(NetworkConfig.paths.reject, { "task_id": taskId })
            .then((res: ApproveTaskResponse) => res);

    }

    search(key: string, inList: Array<any>): Array<any> {
        var newList = [];
        for (let item of inList) {
            if (item.title.indexOf(key) >= 0) {
                newList.push(item);
            }
        }
        return newList;
    }

}